import * as path from "path";
import { Configuration, EnvironmentPlugin } from "webpack";

import { babelOptions, outputDir, plugins, resolve, rules } from "./base";

const cfg: Configuration = {
  entry: { app: ["babel-polyfill", "./main.tsx"] },
  output: { path: outputDir, filename: "[name].js" },
  devServer: { contentBase: outputDir },
  resolve: { extensions: [".ts", ".tsx", ".js", ".jsx"] },
  module: {
    rules: [
      ...Object.values(rules),
      {
        test: /\.tsx?$/,
        use: [
          {
            loader: "awesome-typescript-loader",
            options: {
              silent: true,
              useBabel: true,
              babelOptions,
              babelCore: resolve("@babel/core")
            }
          }
        ]
      },
      {
        test: /\.jsx?$/,
        loader: "babel-loader",
        exclude: /node_modules/,
        options: babelOptions
      },
      {
        test: new RegExp(`src\\${path.sep}(view|edit)\\${path.sep}index\\.tsx?$`),
        loader: path.resolve(__dirname, "../loader/normalize"),
        options: { dev: true }
      }
    ]
  },
  resolveLoader: { modules: module.paths },
  plugins: [plugins.extractText, plugins.envPlugin, plugins.htmlWebpack]
} as any;

export default cfg;
